package cn.ronghuanet.base.service.impl;

import cn.ronghuanet.base.service.IFastdfsService;
import cn.ronghuanet.base.util.FastdfsUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class FastdfsServiceImpl implements IFastdfsService {

    @Value("${ronghuanet.fastdfs.prefixUrl}")
    private String fastdfsUrl;

    @Override
    public String upload(MultipartFile file) throws Exception{
        // 获取文件名称
        // originalFilename 是获取文件名
        // name 获取的是参数名
        String originalFilename = file.getOriginalFilename();

        String extension = FilenameUtils.getExtension(originalFilename);

        // 将文件上传到fastdfs
        String path = FastdfsUtil.upload(file.getBytes(), extension);
        return this.fastdfsUrl+path;
    }

    @Override
    public void delete(String path) {
        // 前端传递的path:http://115.159.217.249:8888/group1/M00/00/3B/rBEAA2SdRrKADnyMAAPfxzBNIxs339.png
        path = path.substring(this.fastdfsUrl.length());
        FastdfsUtil.delete(path);
    }
}
